package com.cn.view;

import com.cn.bean.User;
import com.cn.dao.UserDao;
import com.cn.util.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

/**
 * 登陆界面
 */
public class LoginFrame extends JFrame {
    private JPanel contentPanel;
    private JTextField usernameTextField;
    private JPasswordField passwordTextField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrame frame = new LoginFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LoginFrame() {
        // 通过UIManager设置全局字体
        Font font = new Font("Dialog", Font.PLAIN, 12);
        Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                UIManager.put(key, font);
            }
        }

        setResizable(false);
        setTitle("超市信息管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 320);
        contentPanel = new JPanel();
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPanel);
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon(LoginFrame.class.getResource("/images/background.jpg"));
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(), bg.getIconHeight());
        this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局
        JPanel panel = (JPanel)this.getContentPane();
        panel.setOpaque(false);
        panel.setLayout(new FlowLayout());
        //3.之后把组件和面板添加到窗口面板就可以；
        JLabel titleLabel = new JLabel("超市信息管理系统");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 23));
        titleLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/logo.png")));

        JLabel usernameLabel = new JLabel("用户：");
        usernameLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/name.png")));

        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/password.png")));

        usernameTextField = new JTextField();
        usernameTextField.setColumns(10);
        passwordTextField = new JPasswordField();

        // 点击"登录"按钮执行login()方法
        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的用户名
                String userName = usernameTextField.getText();
                //获取用户输入的密码
                String password = new String(passwordTextField.getPassword());
                // 执行登录
                login(userName,password);
            }
        });
        loginButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/login.png")));

        // 点击"重置"按钮执行reset()方法
        JButton resetButton = new JButton("重置");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/reset.png")));

        panel.add(titleLabel);
        panel.add(usernameLabel);
        panel.add(passwordLabel);
        panel.add(usernameTextField);
        panel.add(passwordTextField);
        panel.add(loginButton);
        panel.add(resetButton);
        this.setSize(bg.getIconWidth(), bg.getIconHeight()+10);

        GroupLayout contentGroupLayout = new GroupLayout(contentPanel);
        ParallelGroup parallelGroup1 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        SequentialGroup sequentialGroup1 = contentGroupLayout.createSequentialGroup();
        ParallelGroup parallelGroup2 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        SequentialGroup sequentialGroup2 = contentGroupLayout.createSequentialGroup();
        sequentialGroup2.addGap(111).addComponent(titleLabel);
        parallelGroup2.addGroup(sequentialGroup2);
        ParallelGroup parallelGroup4 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        parallelGroup4.addComponent(resetButton);
        ParallelGroup parallelGroup5 = contentGroupLayout.createParallelGroup(Alignment.LEADING, false);
        parallelGroup5.addComponent(passwordTextField);
        parallelGroup5.addComponent(usernameTextField, GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE);
        parallelGroup4.addGroup(parallelGroup5);
        SequentialGroup sequentialGroup3 = contentGroupLayout.createSequentialGroup();
        sequentialGroup3.addGap(101);
        ParallelGroup parallelGroup3 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        parallelGroup3.addComponent(usernameLabel).addComponent(passwordLabel).addComponent(loginButton);
        sequentialGroup3.addGroup(parallelGroup3);
        sequentialGroup3.addGap(32);
        sequentialGroup3.addGroup(parallelGroup4);
        parallelGroup2.addGroup(sequentialGroup3);
        sequentialGroup1.addGroup(parallelGroup2);
        sequentialGroup1.addContainerGap(111, Short.MAX_VALUE);
        parallelGroup1.addGroup(sequentialGroup1);
        contentGroupLayout.setHorizontalGroup(parallelGroup1);
        ParallelGroup group4 = contentGroupLayout.createParallelGroup(Alignment.BASELINE);
        group4.addComponent(loginButton);
        group4.addComponent(resetButton);
        ParallelGroup group3 = contentGroupLayout.createParallelGroup(Alignment.BASELINE);
        group3.addComponent(passwordLabel);
        group3.addComponent(passwordTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        SequentialGroup sgroup2 = contentGroupLayout.createSequentialGroup();
        sgroup2.addComponent(usernameLabel);
        sgroup2.addGap(29);
        sgroup2.addGroup(group3);
        ParallelGroup group2 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        group2.addGroup(sgroup2);
        group2.addComponent(usernameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                GroupLayout.PREFERRED_SIZE);
        SequentialGroup sgroup1 = contentGroupLayout.createSequentialGroup();
        sgroup1.addGap(30).addComponent(titleLabel).addGap(26);
        sgroup1.addGroup(group2);
        sgroup1.addGap(36);
        sgroup1.addGroup(group4);
        sgroup1.addContainerGap(60, Short.MAX_VALUE);
        ParallelGroup group1 = contentGroupLayout.createParallelGroup(Alignment.LEADING);
        group1.addGroup(sgroup1);
        contentGroupLayout.setVerticalGroup(group1);
        contentPanel.setLayout(contentGroupLayout);
        this.setLocationRelativeTo(null);
    }

    /**
     * core code
     * 登录
     */
    private void login(String userName,String password) {
        if (StringUtils.isEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！");
            return;
        }
        if (StringUtils.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        // 利用UserDao查询用户
		UserDao userDao = new UserDao();
        User user = userDao.query(userName, password);
        if (user != null) {
            // 关闭当前界面
            dispose();
            // 显示MainFrame
            new MainFrame().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "用户名或者密码错误！");
        }
    }

    /**
     * core code
     * 重置
     */
    private void reset() {
        this.usernameTextField.setText("");
        this.passwordTextField.setText("");
    }
}
